{% extends 'base.html' %}

{% block title %}Create User{% endblock %}
{% block body %}
    {% for u in user %}
        <form class="layui-form layui-form-pane" action="">
            {% csrf_token %}
            <div class="layui-form-item " >
                <label class="layui-form-label">用户ID</label>
                <div class="layui-input-inline">
                    <input type="text" name="ID" required lay-verify="required"
                           autocomplete="off" class="layui-input" value="{{ u.id }}" onfocus="this.blur()"
                           style="background-color: #d9d9d9; cursor: not-allowed;">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" required lay-verify="required" placeholder="Enter username"
                           autocomplete="off" class="layui-input" value="{{ u.username }}" onfocus="this.blur()"
                           style="background-color: #d9d9d9; cursor: not-allowed;">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-inline">
                    <input type="email" name="email" required lay-verify="email" placeholder="Enter email"
                           autocomplete="off" class="layui-input" value="{{ u.email }}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" required
                           placeholder="Enter password"
                           autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">是否启用</label>
                <div class="layui-input-block">
                    <input type="checkbox" {% if u.is_enabled %}checked ="" {% endif %} name="is_enabled"
                           lay-skin="switch" lay-filter="switchTest"
                           lay-text="是|否">
                </div>
            </div>
            <div class="layui-form-item" >
                <label class="layui-form-label">角色分配</label>
                <div class="layui-input-block">
                    {% for role in roles %}
                        <input type="checkbox" name="role[{{ role.id }}]"  title="{{ role.name }}"
                                {% for userRole in u.roles.all %}
                               {% if role.id == userRole.id %}checked="" {% endif %}{% endfor %}>
                    {% endfor %}
                </div>
            </div>

            <div class="layui-form-item" style="margin-top: 1%;">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="editUser">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    {% endfor %}
{% endblock %}
{% block js %}
    <script>
        layui.use(['jquery', 'form', 'layer'], function () {
            var form = layui.form
                , $ = layui.jquery
                , layer = layui.layer;

            form.on('submit(editUser)', function (data) {
                data = data.field;
                const csrf_token = $('[name="csrfmiddlewaretoken"]').val();
                $.ajax({
                    url: "{% url 'auth:user_api' %}",
                    type: 'PUT',
                    data: data,
                    dataType: 'json',
                    headers: {"X-CSRFToken": csrf_token},
                    success: function (res) {
                        if (res.code === 0) {
                            layer.msg(res.msg, {icon: 6});
                            document.location.href = "{% url 'auth:user_list' %}", true;
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (error) {
                        layer.msg(error.responseText, {icon: 5});
                    }
                })
                return false;
            });

        });
    </script>
{% endblock %}